package com.sdy.auth.client.config;

import com.sdy.auth.client.filter.SsoAuthenticationFilter;
import com.sdy.auth.client.filter.SsoAuthorizationFilter;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * <p>File: SsoConfig.java</p>
 * <p>Description: SsoConfig</p>
 *
 * @author zzq
 * @version V1.0
 * @since 2019-05-09
 */
@Configuration
@ComponentScan("com.sdy.auth.client")
public class SsoConfig {
    @Value("${ucsso.server.domain}")
    @Getter
    private String serverDomain;

    @Value("${ucsso.client.domain:}")
    @Getter
    private String clientDomain;

    @Value("${ucsso.client.authorization}")
    @Getter
    private Boolean authorization;

    @Autowired
    private SsoAuthenticationFilter ssoAuthenticationFilter;
    @Autowired
    private SsoAuthorizationFilter ssoAuthorizationFilter;

    public static final Integer SESSION_SECOND_OUT = 20 * 60;
    
    public static final String LOGIN_PAGE_URL = "/login/page";
    public static final String LOGOUT_URL = "/sso/logout";
    
    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(ssoAuthenticationFilter);
        registration.addUrlPatterns("/*");
        registration.setOrder(31);
        return registration;  
    }

    @Bean
    public FilterRegistrationBean filterAuthorizationRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(ssoAuthorizationFilter);
        registration.addUrlPatterns("/*");
        registration.setOrder(32);
        return registration;
    }
}
